﻿using Modules.Corp;
using Modules.Person;
using Modules.Project;
using PmSoft;
using PmSoft.Entity;

namespace ManagementApi.Models.Manage.ProjectGroupMember
{
    /// <summary>
    /// 花名册导出类
    /// </summary>
    public class UseEmployeeExportDto
    {
        /// <summary>
        /// 姓名
        /// </summary>
        public string PersonName { get; set; } = string.Empty;

        /// <summary>
        /// 性别
        /// </summary>
        public string Gender { get; set; } = string.Empty;

        /// <summary>
        /// 身份证号
        /// </summary>
        public string CardNumber { get; set; } = string.Empty;

        /// <summary>
        /// 联系电话
        /// </summary>
        public string Mobile { get; set; } = string.Empty;

        /// <summary>
        /// 家庭地址
        /// </summary>
        public string Address { get; set; } = string.Empty;

        /// <summary>
        /// 民族
        /// </summary>
        public string Nation { get; set; } = string.Empty;

        /// <summary>
        /// 发卡银行名称
        /// </summary>
        public string BankName { get; set; } = string.Empty;

        /// <summary>
        /// 银行卡号
        /// </summary>
        public string BankCardNo { get; set; } = string.Empty;

        /// <summary>
        /// 参建单位
        /// </summary>
        public string CorpName { get; set; } = string.Empty;

        /// <summary>
        /// 班组名称
        /// </summary>
        public string GroupName { get; set; } = string.Empty;

        /// <summary>
        /// 工种类别
        /// </summary>
        public string TypeName { get; set; } = string.Empty;

        /// <summary>
        /// 是否现场管理人员
        /// </summary>
        public string IsManager { get; set; } = string.Empty;

        /// <summary>
        /// 班组进场日期
        /// </summary>
        public string GroupStartDate { get; set; } = string.Empty;

        /// <summary>
        /// 班组离场日期
        /// </summary>
        public string GroupEndDate { get; set; } = string.Empty;

        /// <summary>
        /// 是否班组长
        /// </summary>
        public string IsLeader { get; set; } = string.Empty;

        /// <summary>
        /// 是否签订劳动合同
        /// </summary>
        public string IsContract { get; set; } = string.Empty;

        /// <summary>
        /// 人员进场日期
        /// </summary>

        public string PersonStartDate { get; set; } = string.Empty;

        /// <summary>
        /// 人员离场日期
        /// </summary>
        public string PersonEndDate { get; set; } = string.Empty;
    }

    public static partial class UseEmployeeExportExceptions
    {
        /// <summary>
        /// 将数据库模型转换成导出excel模型
        /// </summary>
        /// <param name="members"></param>
        /// <returns></returns>
        public static IEnumerable<UseEmployeeExportDto> AsUseEmployeeExportDtos(this IEnumerable<Modules.Project.Entitys.ProjectGroupMember> members)
        {
            var projectGroupService = ServiceAccessor.GetService<ProjectGroupService>();
            var projectGroups = projectGroupService.GetProjectGroups(members.Select(a => a.GroupId));

            var projectService = ServiceAccessor.GetService<ProjectService>();
            var projects = projectService.GetProjectInfoByProjectIds(members.Select(a => a.ProjectId));

            var personService = ServiceAccessor.GetService<PersonService>();
            var persons = personService.GetPersons(members.Select(a => a.PersonId));

            var corpService = ServiceAccessor.GetService<CorpService>();
            var corps = corpService.GetCorpsByCorpIds(members.Select(a => a.CorpId));

            var personContractService = ServiceAccessor.GetService<PersonCorpContractService>();
            var personCorpContracts = personContractService.GetPersonCorpContractsByPersonIds(members.Select(a => a.PersonId));

            var personBankService = ServiceAccessor.GetService<PersonBankService>();
            var personBanks = personBankService.GetPersonBanksByIdCards(persons.Select(a => a.CardNumber));

            List<UseEmployeeExportDto> dtos = new();
            foreach (var item in members)
            {
                var projectGroup = projectGroups.FirstOrDefault(a => a.GroupId == item.GroupId);
                var person = persons.FirstOrDefault(a => a.PersonId == item.PersonId);
                var corp = corps.FirstOrDefault(a => a.CorpId == item.CorpId);
                var personCorpContract = personCorpContracts.FirstOrDefault(a => a.CorpId == item.CorpId && a.PersonId == item.PersonId);
                var personBank = personBanks.FirstOrDefault(a => a.IdCard == person?.CardNumber);

                UseEmployeeExportDto dto = new()
                {
                    PersonName = person?.Name ?? string.Empty,
                    Gender = person?.Gender == 1 ? "男" : "女",
                    CardNumber = person?.CardNumber ?? string.Empty,
                    Mobile = person?.Mobile ?? string.Empty,
                    Address = person?.Address ?? string.Empty,
                    Nation = person?.Nation ?? string.Empty,
                    BankName = personBank?.BankName ?? string.Empty,
                    BankCardNo = personBank?.CardNo.ToString() ?? string.Empty,
                    CorpName = corp?.Name ?? string.Empty,
                    GroupName = projectGroup?.Name ?? string.Empty,
                    TypeName = item.Type?.GetDescription() ?? string.Empty,
                    IsManager = item.IsManager ? "是" : "否",
                    GroupStartDate = projectGroup?.StartDate?.ToString("yyyy-MM-dd") ?? string.Empty,
                    GroupEndDate = projectGroup?.EndDate?.ToString("yyyy-MM-dd") ?? string.Empty,
                    IsLeader = item.IsLeader.HasValue ? item.IsLeader.Value ? "是" : "否" : "否",
                    IsContract = personCorpContract != null ? "已签订" : "未签订",
                    PersonStartDate = item.StartDate?.ToString("yyyy-MM-dd") ?? string.Empty,
                    PersonEndDate = item.EndDate?.ToString("yyyy-MM-dd") ?? string.Empty
                };
                dtos.Add(dto);
            }
            return dtos;
        }
    }
}